<?php              
define('DEFAULT_TABLE_ROWS', 1);

$var_cui_furnizor = 0;
$var_id_program = 0;


if($_SERVER['REQUEST_METHOD'] == "POST" and isset($_POST['id_program']) )
{
	$var_id_program = $_POST['id_program'];
}
if($_SERVER['REQUEST_METHOD'] == "GET" and isset($_GET['id_program']) )
{
	$var_id_program = $_GET['id_program'];
}

if($_SERVER['REQUEST_METHOD'] == "POST" and isset($_POST['cui_furnizor']) and ( isset($_POST['add_program']) or isset($_POST['edit_program']) or 
isset($_GET['view_program']) or isset($_POST['view_program'])) )
{
	$var_cui_furnizor = $_POST['cui_furnizor'];
}
else  if($_SERVER['REQUEST_METHOD'] == "GET" and isset($_GET['cui_furnizor']) and ( isset($_GET['add_program']) or isset($_GET['edit_program']) or 
isset($_GET['view_program']) or isset($_POST['view_program']) ) )
{
	$var_cui_furnizor = $_GET['cui_furnizor'];
}


include "includes".DIRECTORY_SEPARATOR."include_user_program_functions.php";
include "includes".DIRECTORY_SEPARATOR."include_user_program_forms_for_tables.php";
include "functions".DIRECTORY_SEPARATOR."utils.php";
include "javascript".DIRECTORY_SEPARATOR."js_functions.php";

$form_fields_with_speficications = array();

for($index = 1; $index <= DEFAULT_TABLE_ROWS; $index+=1) 
{
	foreach( $form_for_table as $form_column_table )
	{
		array_push( $form_fields_with_speficications,$form_column_table);
	}
}

foreach($form_fields_with_speficications as $form_key => $form_value)
{
	if( ! isset($form_fields_with_speficications[$form_key]['colors']) )
			$form_fields_with_speficications[$form_key]['colors'] = array('first' => '#333333', 'second' => 'red');

	if( ! isset($form_fields_with_speficications[$form_key]['field_size']) )
			$form_fields_with_speficications[$form_key]['field_size'] = null;
	if( ! isset($form_fields_with_speficications[$form_key]['max_length']) )
			$form_fields_with_speficications[$form_key]['max_length'] = null;
	if( ! isset($form_fields_with_speficications[$form_key]['regex']) )
			$form_fields_with_speficications[$form_key]['colors'] = null;						  
	if( ! isset($form_fields_with_speficications[$form_key]['can_be_null']) )
			$form_fields_with_speficications[$form_key]['can_be_null'] =  false;
	
	if( ! isset($form_fields_with_speficications[$form_key]['current_value']))
	{
		if( $form_fields_with_speficications[$form_key]['field_type'] == 'text')
		{
			if( isset($form_fields_with_speficications[$form_key]['initial_values']) )
				$form_fields_with_speficications[$form_key]['current_value'] = $form_fields_with_speficications[$form_key]['initial_values'];
			else
				$form_fields_with_speficications[$form_key]['current_value'] =  '';
		}
		else if( $form_fields_with_speficications[$form_key]['field_type'] == 'drop_down_list')
		{
			$form_fields_with_speficications[$form_key]['current_value'] = "neselectat";
		}
	}
	
	$form_fields_with_speficications[$form_key]['current_color'] = $form_fields_with_speficications[$form_key]['colors']['first'];	
}

$form_content = generate_form_content($form_fields_with_speficications);

$erori = "";

if ( isset($_GET['add_program']) or isset($_POST['add_program']) )
{
$get_form=<<<GATA
	<form method="post" action = "user_commission_search.php">
		<table border="0" width="100%" cellspacing="3" cellpadding="3" align="center">
			$form_content
			<input type="hidden" name="add_program" value="true">
			<input type="hidden" name="cui_furnizor" value="$var_cui_furnizor"> 
			<input type="hidden" name="id_program" value="$var_id_program"> 
			<tr><td></td>
				<td><input type ="submit" value = "Salveaza" ></td>
			</tr>
		</table>
	</form>
GATA;
}
else if ( isset($_GET['edit_program']) or isset($_POST['edit_program']) )
{
	$get_form=<<<GATA
	<form method="post" action = "user_commission_search.php">
		<table border="0" width="100%" cellspacing="3" cellpadding="3" align="center">
			$form_content
			<input type="hidden" name="edit_program" value="true">
			<input type="hidden" name="cui_furnizor" value="$var_cui_furnizor"> 
			<input type="hidden" name="id_program" value="$var_id_program"> 
			<tr><td></td>
				<td><input type ="submit" value = "Salveaza" ></td>
			</tr>
		</table>
	</form>
GATA;
} 
else if ( isset($_GET['view_program']) or isset($_POST['view_program']) )
{
		$get_form=<<<GATA
	<form method="post" action = "user_commission_search.php">
		<table border="0" width="100%" cellspacing="3" cellpadding="3" align="center">
			$form_content
			<input type="hidden" name="view_program" value="true">
			<input type="hidden" name="cui_furnizor" value="$var_cui_furnizor"> 
			<input type="hidden" name="id_program" value="$var_id_program"> 
			<tr><td></td>
				<td><input type ="submit" value = "Salveaza" ></td>
			</tr>
		</table>
	</form>
GATA;
} 
else // ADD PROGRAM TAB
{
	$get_form=<<<GATA
	<form method="post" action = "user_commission_search.php">
		<table border="0" width="100%" cellspacing="3" cellpadding="3" align="center">
			$form_content
			<input type="hidden" name="cui_furnizor" value="$var_cui_furnizor"> 
			<tr><td></td>
				<td><input type ="submit" value = "Salveaza" ></td>
			</tr>
		</table>
	</form>
GATA;
}

	

if($_SERVER['REQUEST_METHOD'] == 'GET')
{
	echo $get_form;
}


if($_SERVER['REQUEST_METHOD'] == 'POST')
{
		
	$ok = 1;
		foreach($form_fields_with_speficications as $form_key => $form_value)
		{
		if( isset($_POST[$form_fields_with_speficications[$form_key]['post_name']]))
		{									
			if(empty($_POST[$form_fields_with_speficications[$form_key]['post_name']]) 
			|| ( $_POST[$form_fields_with_speficications[$form_key]['post_name']] == 'neselectat') )
			//if (( $_POST[$form_fields_with_speficications[$form_key]['post_name']] == 'neselectat'))
			{
				# Something went wrong with this form element so an error will be displayed to user
				$ok = 0;
				
				# Change color for element ; default will be red
				$form_fields_with_speficications[$form_key]['current_color'] = "red";
				if( isset($form_fields_with_speficications[$form_key]['colors'] ))
				{
					if( isset($form_fields_with_speficications[$form_key]['colors']['second'] ) )
					{
						$form_fields_with_speficications[$form_key]['current_color'] = 
						$form_fields_with_speficications[$form_key]['colors']['second'];		
					}
				}
				
				# Generate an error if nothing it's provided
				$default_generated_error = "";
				
				if( $form_fields_with_speficications[$form_key]['field_type'] == "text" )
				{
					$default_generated_error = 
					"Nu a fost introdus nimic pentru campul ".$form_fields_with_speficications[$form_key]['post_name']."</br>";
				}
				else if( $form_fields_with_speficications[$form_key]['field_type'] == "drop_down_list" )
				{
					$default_generated_error =
					 "Nu a fost selectat nimic pentru campul ".$form_fields_with_speficications[$form_key]['post_name']."</br>";
				}
				else
				{
					$default_generated_error = "Campul ".$form_fields_with_speficications[$form_key]['post_name']." este gol</br>";
				}
				
				# Add the provided error or the default one
				if( isset($form_fields_with_speficications[$form_key]['errors'] ) )
				{
					if( isset($form_fields_with_speficications[$form_key]['errors']['empty_field_or_no_selection_error'] ))
					{
						$erori = $erori.$form_fields_with_speficications[$form_key]['errors']['empty_field_or_no_selection_error'];					
					}
					else
					{
						$erori = $erori.$default_generated_error;
					}
				}
				else
				{
					$erori = $erori.$default_generated_error;
				}
				
				continue;
			}
			
			# Check regex here
			if ( isset( $form_fields_with_speficications[$form_key]['regex'] ) )
			{
				if ( ! empty( $form_fields_with_speficications[$form_key]['regex'] ) )
				{
					if( preg_match($form_fields_with_speficications[$form_key]['regex'],
					$_POST[$form_fields_with_speficications[$form_key]['post_name']])== 0 )
					{																					
						# Something went wrong with this form element so an error will be displayed to user
						$ok = 0;
						
						# Change color for element ; default will be red
						$form_fields_with_speficications[$form_key]['current_color'] = "red";
						if( isset($form_fields_with_speficications[$form_key]['colors'] ))
						{
							if( isset($form_fields_with_speficications[$form_key]['colors']['second'] ) )
							{
								$form_fields_with_speficications[$form_key]['current_color'] = 
								$form_fields_with_speficications[$form_key]['colors']['second'];		
							}
						}
						
						# Generate an error if nothing it's provided
						$default_generated_error = "";
						
						if( $form_fields_with_speficications[$form_key]['field_type'] == "text" )
						{
							$default_generated_error = "Datele introduse pentru campul ".$form_fields_with_speficications[$form_key]['field_display_name']."sunt invalide</br>";
						}
						else
						{
							$default_generated_error = "Campul ".$form_fields_with_speficications[$form_key]['field_display_name']." este invalid</br>";
						}
						
						# Add the provided error or the default one
						if( isset($form_fields_with_speficications[$form_key]['errors'] ) )
						{
							if( isset($form_fields_with_speficications[$form_key]['errors']['no_regex_pattern_found_error'] ))
							{
								$erori = $erori.$form_fields_with_speficications[$form_key]['errors']['no_regex_pattern_found_error'];					
							}
							else
							{
								$erori = $erori.$default_generated_error;
							}
						}
						else
						{
							$erori = $erori.$default_generated_error;
						}
					
						continue;
					}
				}
			}
			
			# check validation functions
			if ( isset( $form_fields_with_speficications[$form_key]['validation_function'] ) )
			{
				if ( !empty( $form_fields_with_speficications[$form_key]['validation_function'] ) )
				{
					$function_name = $form_fields_with_speficications[$form_key]['validation_function'] ;
					$cif_value = $_POST[$form_fields_with_speficications[$form_key]['post_name']]; 
					
					if($function_name($cif_value) == 0 )
					{																					
						# Something went wrong with this form element so an error will be displayed to user
						$ok = 0;
						
						# Change color for element ; default will be red
						$form_fields_with_speficications[$form_key]['current_color'] = "red";
						if( isset($form_fields_with_speficications[$form_key]['colors'] ))
						{
							if( isset($form_fields_with_speficications[$form_key]['colors']['second'] ) )
							{
								$form_fields_with_speficications[$form_key]['current_color'] = 
								$form_fields_with_speficications[$form_key]['colors']['second'];		
							}
						}
						
						# Generate an error if nothing it's provided
						$default_generated_error = "";
						
						if( $form_fields_with_speficications[$form_key]['field_type'] == "text" )
						{
							$default_generated_error = 
							"Datele introduse pentru campul ".$form_fields_with_speficications[$form_key]['post_name']."
							 sunt invalide</br>";
						}
						else
						{
							$default_generated_error = 
							"Campul ".$form_fields_with_speficications[$form_key]['field_display_name']." este invalid</br>";
						}
						
						# Add the provided error or the default one
						if( isset($form_fields_with_speficications[$form_key]['errors'] ) )
						{
							if( isset($form_fields_with_speficications[$form_key]['errors']['no_regex_pattern_found_error'] ))
							{
								$erori = 
									$erori.$form_fields_with_speficications[$form_key]['errors']['no_regex_pattern_found_error'];					
							}
							else
							{
								$erori = $erori.$default_generated_error;
							}
						}
						else
						{
							$erori = $erori.$default_generated_error;
						}
					
						continue;
					}
				}
			}
			$form_fields_with_speficications[$form_key]['current_value'] = $_POST[$form_fields_with_speficications[$form_key]['post_name']];  											
		}
	}
	
	$mail_address = "-";
	if ( defined('MAIL') )
	{
		$mail_address = MAIL;
	}
	
	if($ok != 1)
	{
		
		if ( isset($_GET['add_program']) or isset($_POST['add_program']) )
		{
			$form_content = generate_form_content($form_fields_with_speficications);
		$form_post=<<<GATA
			<form method="post" action = "user_commission_search.php">
				<table border="0" width="100%" cellspacing="3" cellpadding="3" align="center">
				$form_content
				<input type="hidden" name="add_program" value="true">
				<input type="hidden" name="cui_furnizor" value="$var_cui_furnizor"> 
				<input type="hidden" name="id_program" value="$var_id_program"> 
				<tr>
				<td></td><td><input type ="submit" value = "Salveaza" onclick="this.form.target='_self';return true;" name="comanda"></td>
				</tr>
		</table>
		</form>
GATA;

	echo $form_post;
}
	 else if ( isset($_GET['edit_program']) or isset($_POST['edit_program']) )
		{
			$form_content = generate_form_content($form_fields_with_speficications);
			$form_post=<<<GATA
				<form method="post" action = "user_commission_search.php">
					<table border="0" width="100%" cellspacing="3" cellpadding="3" align="center">
					$form_content
					<input type="hidden" name="edit_program" value="true">
					<input type="hidden" name="cui_furnizor" value="$var_cui_furnizor"> 
					<input type="hidden" name="id_program" value="$var_id_program"> 
					<tr>
					<td></td><td><input type ="submit" value = "Salveaza" onclick="this.form.target='_self';return true;" name="comanda"></td>
				</tr>
		</table>
		</form>
GATA;
			echo $form_post;	
		}
		else
		{
		$form_content = generate_form_content($form_fields_with_speficications);
		$form_post=<<<GATA
			<form method="post" action = "user_commission_search.php">
				<table border="0" width="100%" cellspacing="3" cellpadding="3" align="center">
				$form_content
				<input type="hidden" name="cui_furnizor" value="$var_cui_furnizor"> 
				<input type="hidden" name="id_program" value="$var_id_program">
				<tr>
				<td></td><td><input type ="submit" value = "Salveaza" onclick="this.form.target='_self';return true;" name="comanda"></td>
				</tr>
		</table>
		</form>
GATA;
		echo $form_post;
		}
		
		
		echo "<font color=\"red\">".$erori."</font>";
	}
	else  
	{
		if ( isset($_GET['add_program']) or isset($_POST['add_program']) )
		{
			require_once ('mysql_connect.php');
			
		$u = mysqli_query($dbc,"INSERT INTO `furnizori_programe`
			(`CUI_CF`,`nume_program`,`cod`,`serie_autorizatie`,`nr_autorizatie_program`, `data_autorizatie_program`,
			`tip_program`,`SO_SPP`,`oferta_ucenicie`, `stare_program`,`email_user_comisie`)  
			VALUES 				 	    						   		
			('$var_cui_furnizor','$_POST[nume_program]','$_POST[cod]','$_POST[serie_autorizatie]','$_POST[nr_autorizatie_program]',
			'$_POST[data_autorizatie_program]', '$_POST[tip_program]','$_POST[SO_SPP]','$_POST[oferta_ucenicie]', 'autorizat','$mail_address')");
		
		echo "<br />";	
		echo "<center><h2><font color=\"#00CC00\">Programul a fost adaugat cu succes ! </font></h2></center>";	
		echo "<br />";	
		echo $get_form;
		}
		else if ( isset($_GET['edit_program']) or isset($_POST['edit_program']) )
		{
			require_once ('mysql_connect.php');
				$update_request = mysqli_query($dbc,"UPDATE `furnizori_programe` SET `nume_program`='$_POST[nume_program]',`cod`='$_POST[cod]',`serie_autorizatie`='$_POST[serie_autorizatie]',`nr_autorizatie_program`='$_POST[nr_autorizatie_program]',`data_autorizatie_program`='$_POST[data_autorizatie_program]',`tip_program`='$_POST[tip_program]',`SO_SPP`='$_POST[SO_SPP]',`oferta_ucenicie`='$_POST[oferta_ucenicie]',`state_anc`='neaprobat_anc' WHERE `CUI_CF`='$var_cui_furnizor' AND `id_furnizori_programe`='$var_id_program'");	
			
				if($update_request)
				{
					echo "<br />";	
					echo "<center><h2><font color=\"#00CC00\">Datele au fost modificate cu succes ! </font></h2></center>";	
					echo "<br />";
				}
				else
				{
					echo "<br />";	
					echo "<center><h2><font color=\"#FF0000\">Datele nu au fost modificate ! Reincercati </font></h2></center>";	
					echo "<br />";
					echo $get_form;
				};
		}
		else
		{
			require_once ('mysql_connect.php');
			
			$u = mysqli_query($dbc,"INSERT INTO `furnizori_programe`
				(`CUI_CF`,`nume_program`,`cod`,`serie_autorizatie`,`nr_autorizatie_program`, `data_autorizatie_program`,
				`tip_program`,`SO_SPP`,`oferta_ucenicie`, `stare_program`,`email_user_comisie`)  
				VALUES 				 	    						   		
				('$var_cui_furnizor','$_POST[nume_program]','$_POST[cod]','$_POST[serie_autorizatie]','$_POST[nr_autorizatie_program]',
				'$_POST[data_autorizatie_program]', '$_POST[tip_program]','$_POST[SO_SPP]','$_POST[oferta_ucenicie]', 'autorizat','$mail_address')");
		
		echo "<br />";	
		echo "<center><h2><font color=\"#00CC00\">Program a fost adaugat cu succes ! </font></h2></center>";	
		echo "<br />";	
		echo $get_form;
		}
	}
}
			
?> 